<script lang="ts" setup>

// 点击查看更多跳转到热门动物页面
import type {TheBestAnimalItem} from "@/types/hot";
import {ref, watch} from "vue";
import {ImgErrorType} from "@/enum/imgErrorType";
import type {ImgError} from "@/types/error";

//  动物列表
const props = withDefaults(defineProps<{
  theBestAnimalsList: TheBestAnimalItem[]
}>(), {
  theBestAnimalsList: () => [],
})
// 每个图片对应的加载状态
const imgStatus = ref<boolean[]>([])
// 监听父元素是否数据变化
watch(props.theBestAnimalsList, () => {
  imgStatus.value = new Array(props.theBestAnimalsList.length).fill(false)
}, {deep: true})
imgStatus.value = new Array(props.theBestAnimalsList.length).fill(false)
const onLoadImg = (index: number) => {
  imgStatus.value![index] = true // 完成加载
}
// 点击
const goBest = (animal?: TheBestAnimalItem) => {
  uni.navigateTo({
    url: `/subPkg/best/best?bestId=` + animal?.bestId
  })
}

/*异常处理*/
//图片拿到链接但加载失败
const emit = defineEmits<{
  imgError: [erroImg: ImgError]
}>()
const errorType = ImgErrorType
const onImgError = (index: number, type: ImgErrorType) => {
  emit('imgError', {index, type})
  imgStatus.value![index] = true
}
const currentAnimal = ref(props.theBestAnimalsList[0])
let index = 0
setInterval(() => {
  if (index >= props.theBestAnimalsList.length) {
    index = 0
  }
  currentAnimal.value = props.theBestAnimalsList[index]
  index++
}, 8000)
</script>

<template>
  <view class="best-container">
    <view class="panel-title">
      <text>世界之最</text>
      <view class="text-gray" style="display: flex;align-items: center" @click="goBest">查看更多
        <text class="cuIcon-roundrightfill line-awGreen" style="font-size: 18px;margin-left: 3px"></text>
      </view>
    </view>
    <view>
      <!--    行数随数据变化而变化，列数固定为3列  -->
      <view class="best-box">
        <view v-for="(item, index) in theBestAnimalsList.slice(0,1)" :key="item.animalId" class="box-item">
          <view class="grid-item-box margin-bottom-xs" @click="goBest(item)">
            <uv-image :src="currentAnimal?.img||item?.img" height="140px" mode="aspectFill" radius="10px" width="100%">
              <template #error>
                <image class="img preImg" mode="aspectFit"
                       src="/static/error-img.png"></image>
              </template>
            </uv-image>
            <!--            <image v-show="!imgStatus[index] && item.img" class="img preImg" mode="aspectFit"
                               src="/static/home-selected.png"></image>-->
            <text class="name text-black">{{ currentAnimal?.name || item?.name}}
            </text>
            <text class="desc margin-bottom-sm">{{
                currentAnimal?.desc || item?.desc
              }}
            </text>
          </view>

        </view>
      </view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
.best-container {

  .panel-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    font-size: 13px;
    color: #333333;

    text {
      font-size: 15px;
    }
  }

  .best-box {
    padding: 20rpx;
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    align-items: center;
    gap: 20rpx;
    row-gap: 30rpx;

    .box-item {
      flex: 1;
      min-width: 40%;

      &:nth-child(1),
      &:nth-child(2),
      &:nth-child(6),
      &:nth-child(8),
      &:nth-child(9) {
        flex: 2;
        min-width: 50%;
      }
    }

    .grid-item-box {
      display: flex;
      height: 400rpx;
      flex-direction: column;
      align-items: flex-start;
      justify-content: center;
      position: relative;

      .img {
        flex: 1;
        width: 100%;
        border-radius: 5px;
      }

      .preImg {
      }

      .desc {
        font-size: 28rpx;
        color: #999999;
        height: 30rpx;
        word-wrap: break-word;
        //只显示一行
        overflow: hidden;
      }

      .name {
        font-size: 14px;
        color: #333333;
        margin-top: 5px;
        margin-bottom: 5px;
        font-weight: bold;
        white-space: nowrap;
      }

    }

  }
}
</style>